package com._520it.wms.service.impl;

import com._520it.wms.domain.Depot;
import com._520it.wms.domain.Product;
import com._520it.wms.domain.ProductStock;
import com._520it.wms.mapper.ProductStockMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductStockServiceImpl implements IProductStockService {
    @Autowired
    private ProductStockMapper productStockMapper;

    @Override

    //入库
    public void income(Product product, Depot depot, BigDecimal number, BigDecimal amount) {
        ProductStock ps = productStockMapper.queryByDepotIdAndProdunctId(depot.getId(), product.getId());
        //判断库存是否存在
        if (ps != null) {
            //修改原来的数据
            number = ps.getStoreNumber().add(number);
            amount = ps.getAmount().add(amount);
            //库存的采购价格=总金额/总数量
            BigDecimal price = amount.divide(number, 2, BigDecimal.ROUND_HALF_UP);
            //把值设置进去
            ps.setAmount(amount);
            ps.setPrice(price);
            ps.setStoreNumber(number);
            productStockMapper.updateByPrimaryKey(ps);
        } else {
            ps = new ProductStock();
            ps.setDepot(depot);
            ps.setProduct(product);
            ps.setStoreNumber(number);
            BigDecimal price = amount.divide(number, 2, BigDecimal.ROUND_HALF_UP);
            ps.setPrice(price);
            ps.setAmount(amount);
            productStockMapper.insert(ps);


        }
    }

    @Override
    //出库操作
    public BigDecimal outcome(Product product, Depot depot, BigDecimal number) {
        //根据商品和仓库查询库存
        ProductStock ps = productStockMapper.queryByDepotIdAndProdunctId(depot.getId(), product.getId());
        //没有库存
        if (ps == null) {
            throw new RuntimeException("在" + depot.getName() + "仓库" + product.getName() + "商品没有对应的库存信息");
            //入库<出库 库存不足
        }
        if (ps.getStoreNumber().compareTo(number) < 0) {
            throw new RuntimeException("在" + depot.getName() + "仓库" + product.getName() + "商品剩余" + ps.getStoreNumber());
        }
        //正常出库剩余数量
        number = ps.getStoreNumber().subtract(number);
        BigDecimal amount = number.multiply(ps.getPrice()).setScale(2, BigDecimal.ROUND_HALF_UP);
        ps.setStoreNumber(number); //数量
        ps.setAmount(amount);//金额
        productStockMapper.updateByPrimaryKey(ps);
        //通过库存商品和仓库信息得到库存价格
        return ps.getPrice();

    }

    @Override
    //分页查询
    public PageResult query(QueryObject qo) {
        //查询总数
        int totalCount = productStockMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
            //查询结果集
        List<ProductStock> list =productStockMapper.queryForList(qo);
        return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),list,totalCount);


    }
}